
<?php 
require_once '../functions.php';
xiu_get_current_user();

function user_add(){

   $checkemail = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";

   if (empty($_POST['email']) || !preg_match($checkemail, $_POST['email'])) {
    $GLOBALS['message'] = '请输入合法邮箱';
    return;
   }
   if (empty($_POST['slug'])) {
    $GLOBALS['message'] = '请输入别名';
    return;
   }
   if (empty($_POST['nickname'])) {
    $GLOBALS['message'] = '请输入昵称';
    return;
   }
   if (empty($_POST['password'])) {
    $GLOBALS['message'] = '请输入密码';
    return;
   }
   $email = $_POST['email'];
   $slug = $_POST['slug'];
   $nickname = $_POST['nickname'];
   $password= $_POST['password'];
   //添加到数据库中
   $rows = xiu_execute("insert into users values(null,'{$slug}','{$email}','{$password}','{$nickname}','{$_POST['avatar']}',null,'{$_POST['status']}');");
   if ($rows <= 0) {
   $GLOBALS['message'] = '添加失败';
   $GLOBALS['success'] = false;
   return;
   }
   if ($rows > 0) {
   $GLOBALS['message'] = '添加成功';
   $GLOBALS['success'] = true;
   return;
   }
}
function user_edit(){
  global $current_user_edit;
  $checkemail = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";

   if (!preg_match($checkemail, $_POST['email'])) {
    $GLOBALS['message'] = '请输入合法邮箱';
    return;
   }
   $id = $current_user_edit['id'];
   $email = empty($_POST['email']) ? $current_user_edit['email'] : $_POST['email'];
   $current_user_edit['email'] = $email;
   $slug = empty($_POST['slug']) ? $current_user_edit['slug'] : $_POST['slug'] ;
   $current_user_edit['slug'] = $slug;
   $nickname = empty($_POST['nickname']) ? $current_user_edit['nickname'] : $_POST['nickname'];
   $current_user_edit['nickname'] = $nickname;
   $password= empty($_POST['password']) ? $current_user_edit['password'] : $_POST['password'];
   $current_user_edit['password'] = $password;
   //更新数据库中
   $rows = xiu_execute("update users set slug='{$slug}',email='{$email}',password ='{$password}',nickname ='{$nickname}' where id ={$id}");
   if ($rows <= 0) {
   $GLOBALS['message'] = '更新失败';
   $GLOBALS['success'] = false;
   return;
   }
   if ($rows > 0) {
   $GLOBALS['message'] = '更新成功';
   $GLOBALS['success'] = true;
   return;
   }
}
if (empty($_GET['id'])) {
  //添加
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  user_add();
 }
 } else {
  //编辑一条
  $current_user_edit = xiu_fetch_one('select * from users where id =' . $_GET['id']);
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  user_edit();
 }

}

$users = xiu_fetch_all('select * from users;');


 ?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Users &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
    <?php include 'inc/navbar.php' ?>
    <div class="container-fluid">
      <div class="page-title">
        <h1>用户</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <?php if (isset($message)): ?>
        <?php if ($success): ?>
          <div class="alert alert-success">
        <strong>成功！</strong><?php echo $message ?>
      </div>        
        <?php else: ?>
        <div class="alert alert-danger">
        <strong>错误！</strong><?php echo $message ?>
      </div>
    <?php endif ?>
      <?php endif ?>
      <div class="row">
        <div class="col-md-4">
         <?php if (isset($current_user_edit)): ?>
          <form action="<?php echo $_SERVER['PHP_SELF'] ?>?id=<?php echo $current_user_edit['id']; ?>" method = 'post'>
            <h2>编辑《<?php echo $current_user_edit['nickname']; ?>》</h2>
            <div class="form-group">
              <label for="email">邮箱</label>
              <input id="email" class="form-control" name="email" type="email" placeholder="邮箱" value="<?php echo $current_user_edit['email']; ?>">
            </div>
            <div class="form-group">
              <label for="slug">别名</label>
              <input id="slug" class="form-control" name="slug" type="text" placeholder="slug" value="<?php echo $current_user_edit['slug']; ?>">
              <p class="help-block">https://zce.me/author/<strong>slug</strong></p>
            </div>
            <div class="form-group">
              <label for="nickname">昵称</label>
              <input id="nickname" class="form-control" name="nickname" type="text" placeholder="昵称" value="<?php echo $current_user_edit['nickname']; ?>">
            </div>
            <div class="form-group">
              <label for="password">密码</label>
              <input id="password" class="form-control" name="password" type="text" placeholder="密码" value="<?php echo $current_user_edit['password']; ?>">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">保存</button>
            </div>
          </form>
          <?php else: ?>
          <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method = 'post'>
            <h2>添加新用户</h2>
            <div class="form-group">
              <label for="email">邮箱</label>
              <input id="email" class="form-control" name="email" type="email" placeholder="邮箱">
            </div>
            <div class="form-group">
              <label for="slug">别名</label>
              <input id="slug" class="form-control" name="slug" type="text" placeholder="slug">
              <p class="help-block">https://zce.me/author/<strong>slug</strong></p>
            </div>
            <div class="form-group">
              <label for="nickname">昵称</label>
              <input id="nickname" class="form-control" name="nickname" type="text" placeholder="昵称">
            </div>
            <div class="form-group">
              <label for="password">密码</label>
              <input id="password" class="form-control" name="password" type="text" placeholder="密码">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">添加</button>
            </div>
          </form>
         <?php endif ?>
        </div>
        <div class="col-md-8">
          <div class="page-action">
            <!-- show when multiple checked -->
            <a id="btn_delete" class="btn btn-danger btn-sm" href="/admin/users-delete.php" style="display: none">批量删除</a>
          </div>
          <table class="table table-striped table-bordered table-hover">
            <thead>
               <tr>
                <th class="text-center" width="40"><input type="checkbox"></th>
                <th class="text-center" width="80">头像</th>
                <th>邮箱</th>
                <th>别名</th>
                <th>昵称</th>
                <th>状态</th>
                <th class="text-center" width="100">操作</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($users as $items): ?>
                <tr>
                <td class="text-center"><input type="checkbox" data-id=<?php echo $items['id']; ?>></td>
                <td class="text-center"><img class="avatar" src="<?php echo $items['avatar']; ?>"></td>
                <td><?php echo $items['email']; ?></td>
                <td><?php echo $items['slug']; ?></td>
                <td><?php echo $items['nickname']; ?></td>
                <td><?php echo $items['status']; ?></td>
                <td class="text-center">
                  <a href="users.php?id=<?php echo $items['id']; ?>" class="btn btn-default btn-xs">编辑</a>
                  <a href="users-delete.php?id=<?php echo $items['id']; ?>" class="btn btn-danger btn-xs">删除</a>
                </td>
              </tr>
              <?php endforeach ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
  <?php $current_page = 'users'; ?>
  <?php include 'inc/sidebar.php'; ?>

  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>NProgress.done()</script>
  <script>
    $(function($){
      //表中任意一个元素发生变化时
       var $tbodyCheckbox = $('tbody input');
       var $btnDelete = $('#btn_delete');
       //定义一个数组用来表示被选中的
       var allCheck = [];
       $tbodyCheckbox.on('change',  function() {
         var $id = $(this).data('id');
         console.log($id);
         //根据有没有选中这个checkbox决定是删除还是增加
         if ($(this).prop('checked')) {
          allCheck.push($id)
         }else {
          allCheck.splice(allCheck.indexOf($id), 1);
         }
         //根据剩下所选的checkbox决定是否显示批量删除按钮
         allCheck.length ? $btnDelete.fadeIn() : $btnDelete.fadeOut();
         $btnDelete.prop('search', 'id=' + allCheck);
       })
    });
  </script>
</body>
</html>
